App({

  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function () {

  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function (options) {

  },

  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function () {

  },

  /**
   * 当小程序发生脚本错误loginlo，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function (msg) {

  },
// 分享配置的图片和标题
  // shareConfig:{
  //   1:{
  //     id:1,
  //     img_url:'',
  //     title:"通用分享"
  //   },
  //   2:{
  //     id: 2,
  //     img_url: '',
  //     title: "中奖时弹出红包点击邀请好友的分享"
  //   },
  //   3:{
  //     id: 3,
  //     img_url: '',
  //     title: "未中奖时弹出红包点击邀请好友的分享"
  //   }	,
  //   4:{
  //     id: 4,
  //     img_url: '',
  //     title: "抽奖成功弹框后点击确定的分享"
  //   }	,
  //   5:{
  //     id: 5,
  //     img_url: '',
  //     title: "抽奖积分不足时未完成分享任务点击确定的分享"
  //   }	,
  //   6:{
  //     id: 6,
  //     img_url: '',
  //     title: "每日任务拿积分"
  //   }	,
  //   7:{
  //     id: 7,
  //     img_url: '',
  //     title: "充值成功后弹框点击确定的分享"
  //   }	,
  // },
  shareCconfig:'',

  globalData: {
    // appUrl: 'https://api.legu17.com/api/',
    appUrl: 'https://test.legu17.com/api/'
  },
  userInfo: {

  },
  request: function (url, dataObj, callback1, callback2, callback3) {
    var that = this;
    if (wx.getStorageSync('uid')) {
      dataObj.uid = wx.getStorageSync('uid');
    }
    wx.request({  
      url: that.globalData.appUrl + url,
      data: dataObj,
      success: function (res) {
        console.log(res);
        if (res.data.errno == 0) {
          if(callback1){
            callback1(res);
          }
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon:'none',
          })
          if (callback2) {
            callback2(res);
          }
        }
      },
      complete: function (res) {
        if (callback3) {
          callback3(res);
        }
      }
    })
  },
  post: function (url, dataObj, callback1, callback2, callback3) {
    var that = this;
    wx.request({
      url: that.globalData.appUrl + url,
      data: dataObj,
      method: 'post',
      header: {
        "Content-Type": "application/json; charset=utf-8" //post请求设置
      },
      success: function (res) {
        if (res.data.errno == 0) {
          callback1(res.data);
        } else {
          if (callback2) {
            callback2(res.data);
          }
        }
      },
      complete: function (res) {
        if (callback3) {
          callback3(res);
        }
      }
    })
  },
  // 登陆注册逻辑
  codeLogin: function () {
    var self = this;
    var p = new Promise(function (resolve, reject) {
      var uid = wx.getStorageSync('uid');
      if (!uid) {
        var url = 'login/login';
        wx.login({
          success: function (res) {
            var obj = {
              code: res.code,
            }
            if (wx.getStorageSync('parent_id')) {
              obj.parent_id = wx.getStorageSync('parent_id');
            }
            wx.request({
              url: self.globalData.appUrl + url,
              data: obj,
              method: 'post',
              header: {
                "Content-Type": "application/json; charset=utf-8" //post请求设置
              },
              success: function (res) {
                if (res.data.errno == 0) {
                  var uid = res.data.data.user_info.user_id,
                    user_info = res.data.data.user_info;
                  wx.setStorageSync('uid',uid);
                  wx.setStorageSync('user_info', user_info);
                  resolve(user_info);
                }
              },
            })
          }
        })
      } else {
        self.uid = uid;
        self.user_info = wx.getStorageSync('user_info');
        resolve(self.user_info);
      }
    });
    return p;
  },
  // 授权注册
  authuorLogin: function (iv, encryptedData, callback) {
    var self = this;
    var p = new Promise(function (resolve, reject) {
      var uid = wx.getStorageSync('uid');
      var url = 'login/login';
      wx.login({
        success: function (res) {
          var obj = {
            code: res.code,
            iv: iv,
            encryptedData: encryptedData,
          }
          if (wx.getStorageSync('parent_id')) {
            obj.parent_id = wx.getStorageSync('parent_id');
          }
          wx.request({
            url: self.globalData.appUrl + url,
            data: obj,
            method: 'post',
            header: {
              // "Content-Type": "application/json; charset=utf-8" //post请求设置
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              if (res.data.errno == 0) {
                var uid = res.data.data.user_info.user_id,
                  user_info = res.data.data.user_info;
                wx.setStorageSync('uid', uid);
                wx.setStorageSync('user_info', user_info);
                resolve(user_info);
                if (callback) {
                  callback();
                }
              } else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none'
                })
              }
            },
          })
        }
      })
    });
    return p;


  },
  // 小程序跳转
  goProgram: function (appId, path, extraData, callback) {
    wx.navigateToMiniProgram({
      appId: appId,
      path: path,
      extraData: extraData,
      envVersion: 'release',
      success(res) {
        // 打开成功
        if (callback) {
          callback();
        }
      }
    })
  },

})
//0817

